package com.eonmind.ai.controller;


import com.eonmind.ai.core.draw.platform.mdJounery.model.MdTaskQueryResponse;
import com.eonmind.ai.dto.AiDrawDto;
import com.eonmind.ai.service.impl.AiBiService;
import com.eonmind.ai.service.impl.AiDrawService;
import com.eonmind.common.contract.data.response.PageResponse;
import com.eonmind.common.contract.data.response.ResponseEntity;
import com.eonmind.common.security.annotations.UserInfo;
import com.eonmind.user.contract.api.UserNoticeFeignService;
import com.eonmind.user.contract.dto.UserInfoData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * AI绘画
 */
@Slf4j
@RestController
public class AiDrawController {
    @Resource
    private AiDrawService aiDrawService;
    @Resource
    private AiBiService aiBiService;
    @Autowired
    private UserNoticeFeignService userNoticeFeignService;


    /**
     * 提交绘画任务（文生图）
     *
     * @param aiDrawTaskRequest 任务请求
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/ai/draw/mj/submit")
    public ResponseEntity<MdTaskQueryResponse> submitDrawTask(
            UserInfoData userInfoData,
            @RequestBody @Validated AiDrawDto.AiDrawMjTaskRequest aiDrawTaskRequest) {
        MdTaskQueryResponse mdTaskQueryResponse = aiDrawService.submitMjTask(userInfoData, aiDrawTaskRequest);
        aiBiService.updateUserConsumeHistory(userInfoData.getId(), 0L, 1L, 0L, "");
        return ResponseEntity.success(mdTaskQueryResponse);
    }


    /**
     * 查询任务进度
     *
     * @param aiDrawTaskQueryRequest 获取任务结果请求
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/ai/draw/mj/query")
    public ResponseEntity<AiDrawDto.MjDrawTaskQueryResponse> getDrawTaskResult(
            UserInfoData userInfoData,
            @RequestBody @Validated AiDrawDto.AiDrawTaskQueryRequest aiDrawTaskQueryRequest) {
        AiDrawDto.MjDrawTaskQueryResponse taskQueryResponse = aiDrawService.queryMjTask(userInfoData, aiDrawTaskQueryRequest);
        return ResponseEntity.success(taskQueryResponse);
    }

    /**
     * 分页查询用户绘画任务
     *
     * @param aiDrawPageRequest 分页请求
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/ai/draw/mj/page")
    public ResponseEntity<PageResponse<AiDrawDto.MjDrawTaskQueryResponse>> page(
            UserInfoData userInfoData,
            @RequestBody @Validated AiDrawDto.AiDrawPageRequest aiDrawPageRequest) {
        PageResponse<AiDrawDto.MjDrawTaskQueryResponse> aiDrawPageResponse = aiDrawService.pageMjDraw(userInfoData, aiDrawPageRequest);
        return ResponseEntity.success(aiDrawPageResponse);
    }

    /**
     * 删除绘图任务
     *
     * @param drawTaskDelRequest
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/ai/draw/del")
    public ResponseEntity<String> del(
            UserInfoData userInfoData,
            @RequestBody @Validated AiDrawDto.DrawTaskDelRequest drawTaskDelRequest) {
        aiDrawService.delDrawTask(userInfoData, drawTaskDelRequest);
        return ResponseEntity.success("删除成功");
    }


    /**
     * 执行MJ命令
     *
     * @param aiDrawMjCommandRequest
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/ai/draw/mj/command")
    public ResponseEntity<MdTaskQueryResponse> executeCommandTask(
            UserInfoData userInfoData,
            @RequestBody @Validated AiDrawDto.AiDrawMjCommandRequest aiDrawMjCommandRequest) {
        MdTaskQueryResponse mdTaskQueryResponse = aiDrawService.executeMjCommandTask(userInfoData, aiDrawMjCommandRequest);
        return ResponseEntity.success(mdTaskQueryResponse);
    }

    /**
     * 手动同步一次绘画任务进度
     *
     * @param taskSyncRequest
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/ai/draw/mj/sync")
    public ResponseEntity<String> customSyncMjTaskProgress(UserInfoData userInfoData, @RequestBody @Validated AiDrawDto.AiDrawMjTaskSyncRequest taskSyncRequest) {
        aiDrawService.syncMjTaskProgress(userInfoData, taskSyncRequest);
        return ResponseEntity.success();
    }

}
